package com.nico.chatvoiceview

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.support.graphics.drawable.VectorDrawableCompat
import android.util.AttributeSet
import android.view.View
import com.nico.voiceview.R

class ChatItemView : View {

    sealed interface Type {
        object INIT : Type

        // 取消
        object CANCEL : Type

        // 发送
        object SEND : Type

        object RESULT : Type
        object ORIGINAL : Type
    }

    private var mMinHeight = 0
    private var mMinWidth = 0
    private var mType: Type = Type.INIT
    private var mChecked: Boolean = false
    private var mText: String = "完全"
    private var mTextColor = Color.WHITE

    //需要换成文字图标
    private val rightVoiceIconCheck =
        VectorDrawableCompat.create(context.resources, R.drawable.ic_baseline_translate_check, null)

    private val rightVoiceIconUncheck =
        VectorDrawableCompat.create(
            context.resources,
            R.drawable.ic_baseline_translate_uncheck,
            null
        )
    val paint = Paint()
    private val leftVoiceIconUncheck =
        VectorDrawableCompat.create(context.resources, R.drawable.ic_baseline_close_uncheck, null)

    private val leftVoiceIconcheck =
        VectorDrawableCompat.create(context.resources, R.drawable.ic_baseline_close_check, null)


    private val resultSendEnableIcon =
        VectorDrawableCompat.create(context.resources, R.drawable.check_icon_green, null)


    private val originalSendEnableIcon =
        VectorDrawableCompat.create(context.resources, R.drawable.ic_baseline_original, null)


    constructor(context: Context) : this(context, null) {
    }

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0) {
    }

    @SuppressLint("InflateParams", "ClickableViewAccessibility")
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        initView()
    }

    private fun initView() {
        mMinWidth = 100
        mMinHeight = 100
    }

    public fun setType(type: Type) {
        mType = type
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val vectorDrawable: VectorDrawableCompat
        val paint = paint.apply {
            textSize = 38f
            color = Color.BLACK
        }

        val fontMetrics = paint.fontMetrics

        // 测量文本宽度
        val textWidth = paint.measureText(mText)
        val textHeight = fontMetrics.bottom - fontMetrics.top
        when (mType) {
            Type.CANCEL -> {
                vectorDrawable = if (mChecked) {
                    leftVoiceIconcheck!!
                } else {
                    leftVoiceIconUncheck!!
                }
            }

            Type.SEND -> {
                vectorDrawable = if (mChecked) {
                    rightVoiceIconCheck!!
                } else {
                    rightVoiceIconUncheck!!
                }
            }

            Type.RESULT -> {
                vectorDrawable = resultSendEnableIcon!!
            }

            Type.ORIGINAL->{
                vectorDrawable =  originalSendEnableIcon!!
            }
            else -> {
                vectorDrawable = leftVoiceIconUncheck!!
            }
        }
        val width = vectorDrawable.intrinsicWidth
        val height = vectorDrawable.intrinsicHeight
        val measuredWidth = measuredWidth
        val measuredHeight = measuredWidth+textHeight/2

        var radius = measuredWidth / 2
        if (mChecked) {
            paint.color = Color.WHITE
            canvas.drawCircle(radius.toFloat(), radius.toFloat(), radius.toFloat(), paint)
        } else {
            paint.color = Color.parseColor("#3C3C3C")
            canvas.drawCircle(radius.toFloat(), radius.toFloat(), radius.toFloat() - 10f, paint)
        }
        if (measuredWidth > width) {
            vectorDrawable.setBounds(
                (measuredWidth - width) / 2,
                (measuredWidth - height) / 2,
                (measuredWidth - width) / 2 + width,
                (measuredWidth - width) / 2 + width
            )
        } else {
            vectorDrawable.setBounds(0, 0, width, height)
        }
        vectorDrawable.draw(canvas)
        val color = mTextColor
        paint.color=color
        canvas.drawText(mText,(measuredWidth-textWidth)/2,measuredHeight+20f,paint)

    }

    public fun setText(text:String){
        mText=text
        invalidate()
    }

    public fun setColor(textColor:Int){
        mTextColor=textColor
        invalidate()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        val wResualt = if (widthMode == MeasureSpec.EXACTLY) {
            widthSize
        } else {
            Math.min(widthSize, mMinWidth)
        }
        val hResualt = if (heightMode == MeasureSpec.EXACTLY) {
            heightSize
        } else {
            Math.min(heightSize, mMinHeight)
        }

        val paint = paint.apply {
            textSize = 48f
            color = Color.BLACK
        }

        val fontMetrics = paint.fontMetrics
        val textHeight = fontMetrics.bottom - fontMetrics.top
        setMeasuredDimension(wResualt, (wResualt+textHeight).toInt())
    }


    public fun setChecked(checked: Boolean) {
        mChecked = checked
        invalidate()
    }

}